package com.supermarket.service.impl;

import com.alibaba.excel.EasyExcel;
import com.supermarket.entity.Staff;
import com.supermarket.listener.StaffListener;
import com.supermarket.mapper.StaffMapper;
import com.supermarket.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;
@Override
public List<Staff> findAll() {return staffMapper.findAll();}
@Override
    public Integer findCount(){return  staffMapper.findCount();}
    @Override
    public void add(Staff staff) {
        staffMapper.add(staff);
    }
    @Override
    public void deleteById(String staffID){ staffMapper.deleteById(staffID);}
    @Override
    public void updateStaff(Staff staff) {
        staffMapper.updateStaff(staff);
    }
    @Override
    public void deleteByIds(Integer [] ids){  staffMapper.deleteByIds(ids);}

    @Override
    public List<Staff> findByIds(Integer[] ids) {
        return staffMapper.findByIds(ids);
    }
    @Override
    public void save(MultipartFile file) {
        // 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
        // 写法3：
        //fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";


        InputStream inputStream = null;

        //通过流来读取上传的文件
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, Staff.class, new StaffListener(staffMapper)).sheet().doRead();

    }
}
